<!--
 * @Descripttion: 
 * @LastEditors: xzh
 * @LastEditTime: 2023-10-14 22:26:16
-->
<template>
  <div class="dile-table-tools" v-if="toolBtnArr.length > 0">
    <!-- 刷新按钮 -->
    <el-button
      v-if="!!propsParam.moduleToolCode"
      type="success"
      plain
      :icon="Refresh"
      @click="handleRefreshTable"
    />
    <!-- 传入工具栏按钮 -->
    <div class="dile-table-tools-content">
      <div v-for="position in ['left', 'right']" :key="position">
        <slot :name="position == 'left' ? 'preBtn' : position + 'Btn'" />
        <ToolsItem
          v-for="(toolBtn, index) in toolBtnArr.filter(
            (v:object_Inf) => (v.position || 'left') == position
          )"
          :key="index"
          :toolBtnItem="toolBtn"
          @toolBtnClick="handleToolBtnClick"
        />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, inject, provide, computed } from "vue";
import type { object_Inf } from "@/utils/typings/index.type";
import { btnTypeEnum, propsParam_Inf, tableTool_Inf } from "../index.type";
import { Refresh } from "@element-plus/icons-vue";
import ToolsItem from "./tools-item/index.vue";
import DilePower_Constants from "@/utils/constants/modulePower";
import Dile_StoreAction from "@/utils/cache/store";
let emit = defineEmits(["resetPagination"]);
/**
 * @Author: xzh
 * @Descripttion:表格传入参数
 * @Param:
 */
let propsParam = reactive<propsParam_Inf | object_Inf>(
  inject("propsParam") || {}
);

/**
 * @Author: xzh
 * @Descripttion: 获取工具类
 * @Param:
 * @param {*} computed
 */
let toolBtnArr = computed(() => {
  let { moduleToolCode, tableToolArr } = propsParam || {};
  if (tableToolArr?.length > 0) {
    return tableToolArr;
  }
  //获取当前角色权限
  let { isEnable, powerSet = {} } = Dile_StoreAction.currentUserRoleInfo(),
    //获取当前模块按钮权限
    { toolBtnPowerArr } =
      powerSet?.modulePowerArr?.find(
        (x: object_Inf) => x.module == moduleToolCode.replace("Tools", "")
      ) || {};
  //匹配权限中按钮
  return (
    DilePower_Constants[moduleToolCode]?.filter((x: object_Inf) =>
      !!isEnable ? !!toolBtnPowerArr?.includes(x.code) : true
    ) || []
  );
});

/**
 * @Author: xzh
 * @Descripttion:按钮点击事件
 * @Param:
 */
let handleToolBtnClick = (event: object_Inf) => {
  let { toolBtnClick = () => {} } = propsParam.emitEvents,
    { customBehavior, btnType }: tableTool_Inf = event.item || {};
  //筛选按钮点击调刷新表格事件
  ([btnTypeEnum.refresh, btnTypeEnum.keyWordFilter] as Array<string>).includes(
    btnType
  )
    ? //刷新表格
      handleRefreshTable()
    : //触发按钮点击回调事件
      !!customBehavior && toolBtnClick(event);
};

/**
 * @Author: xzh
 * @Descripttion: 刷新表格
 * @Param:
 */
let handleRefreshTable = () => {
  let { refreshTable = () => {} } = propsParam.emitEvents;
  //重置分页
  emit("resetPagination");
  //刷新表格
  refreshTable();
};

/**
 * @Author: xzh
 * @Descripttion:工具栏参数
 * @Param:
 */
let toolsParam = reactive<object_Inf>({
  className: "", //类型
  condition: [], //条件
  keyWord: "", //关键字
  toolBtnClick: handleToolBtnClick,
});
provide("toolsParam", toolsParam);

//暴露数据或方法
defineExpose({
  toolsParam,
});
</script>
<style lang="scss" scoped></style>
